﻿@using FFF.Test.Entity.FW.View;
@using Microsoft.AspNetCore.Http;
@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/_Index_Layout.cshtml";
    ViewFWUser currentUserInfo = base.Context.Session.GetUserInfo();
    // 改
}
@if (currentUserInfo.IsHaveRights("FW_Action", "00"))
{
    @section styles{
    }
    @section scripts{
        @*<script src="~/js/views/Main/Home/index.js?rnd=@DateTime.Now.Ticks"></script>*@
    }

    <div data-toggle="topjui-layout" data-options="fit:true">
        @*<div data-options="region:'north',title:'@L("查找")',split:true" style="height:150px;padding-left:10px">
                <form id="mySearchForm" style="display:none">
                    <table style="width:100%;border-collapse:separate; border-spacing:0px 10px;">
                        <tr>

                            <td class="label"> @L("对应的功能")</td>
                             <td>
                                 <input  id="mySearchFormFunctionGUID" type="text" name="functionGUID" data-toggle="topjui-combobox" data-name="functionGUID" data-op="eq" data-datatype="uniqueidentifier"
                                        data-options="required:false,width:250,
                                        editable: true,
                                        valueField:'id',
                                        textField:'text',
                                        loader: $.loader.comboxLoader,
                                        mode: 'remote',
                                        queryParams:
                                        {
                                            viewName:'View_FW_function',
                                            comBoxValueName:'functionName'

                                        }">
                             </td>

                            <td class="label">@L("动作代码")</td>
                            <td>
                                <input id="mySearchFormActionCode" type="text"  data-toggle="topjui-textbox" data-name="actionCode" data-op="like"  data-datatype="nvarchar" data-options="width:250">
                            </td>

                            <td class="label">@L("动作名称")</td>
                            <td>
                                <input id="mySearchFormActionName" type="text"  data-toggle="topjui-textbox" data-name="actionName" data-op="like"  data-datatype="nvarchar" data-options="width:250">
                            </td>

                            <td class="label">@L("动作说明")</td>
                            <td>
                                <input id="mySearchFormActionRemark" type="text"  data-toggle="topjui-textbox" data-name="actionRemark" data-op="like"  data-datatype="nvarchar" data-options="width:250">
                            </td>

                        </tr>
                    </table>
                    <div style="position:absolute;right:20px; bottom:10px;">
                      <a href="javascript:void(0)" data-toggle="topjui-linkbutton" data-options="iconCls:'fa fa-search-minus', btnCls:'topjui-btn-search'" onclick="searchQuery()">@L("搜索(enter)")</a>
                        <a href="javascript:void(0)" data-toggle="topjui-linkbutton" data-options="iconCls:'fa fa-mail-reply', btnCls:'topjui-btn-clear'" onclick="$('#mySearchForm').iForm('clear')">@L("清空(esc)")</a>
                    </div>
                </form>
            </div>*@

        <div data-options="region:'center',title:'',fit:false,split:true,border:false">
            <table id="myDg"></table>

            <div id="myDg-toolbar" class="topjui-toolbar" data-options="
        grid:{
            type:'datagrid',
            id:'myDg'
            }">

                @if (currentUserInfo.IsHaveRights("FW_Action", "01"))
                {
                    <a id="add" href="javascript:void(0)" data-toggle="topjui-menubutton"
                       data-options="iconCls:'fa fa-plus',onClick: oepnAddDiag">
                        @L("新增")
                    </a>
                }

                @if (currentUserInfo.IsHaveRights("FW_Action", "03"))
                {
                    <a href="javascript:void(0)" data-toggle="topjui-menubutton" data-options="
                method:'doAjax',
                btnCls:'topjui-btn-delete',
                extend: '#myDg-toolbar',
                iconCls:'fa fa-trash',
                confirmMsg:'@L("确定删除选中的数据吗")?',
                grid: {uncheckedMsg:'@L("请勾选要删除的数据")',param:'id:id'},
                url:'/api/services/app/FWAction/BatchDelete' ">@L("删除")</a>
            }

                <a href="javascript:void(0)"
                   data-toggle="topjui-menubutton"
                   data-options="
                method:'filter',
                extend: '#myDg-toolbar',
                btnCls:'topjui-btn-filter'">@L("过滤")</a>

                <a href="javascript:void(0)"
                   data-toggle="topjui-menubutton"
                   data-options="
                   method:'search',
                   extend: '#myDg-toolbar' ,
                   btnCls:'topjui-btn-query'">@L("查询")</a>

                @if (currentUserInfo.IsHaveRights("FW_Action", "92"))
                {
                    <a href="javascript:void(0)"
                       data-toggle="topjui-menubutton"
                       data-options="
                    method:'export',
                    extend: '#myDg-toolbar',
                    iconCls:'fa fa-cloud-download',
                    btnCls:'topjui-btn-export',
                    url:'/api/services/app/FWAction/GetGridExcel'">@L("导出")</a>
                }

                <a href="javascript:void(0)"
                   data-toggle="topjui-menubutton"
                   data-options="
                    btnCls:'topjui-btn-black',
                    onClick:initActions">@L("初始化权限点")</a>



                    @*<a href="javascript:void(0)"
            data-toggle="topjui-menubutton"
            data-options="
                btnCls:'topjui-btn-black',
                onClick:myQuery">@L("自定义方法")</a>*@
                </div>
        </div>
    </div>


<!--自定义修改,尽量不要改框架的代码,修改全放这里,如果要修改框架代码,尽量在这里写个方法,在框架调用-->
<script>
    var fWActionIndex={
        queryObj: $.dm.getQueryObj(), // URL的参数
        buInfo: $.dm.getBuInfo(), // 门店信息
        userInfo: $.dm.getUserInfo(), // 用户信息
        appInfo:$.dm.getAppInfo(), // 系统信息
        selfClick:function (){ // 自定义按钮对应的方法
            alert("123");
            // 选择一行
            $('#myDg').datagrid('selectRow', 1);
            // 提示信息
            //$.iMessager.alert('自定义方法', '自定义方法被执行了！', 'messager-info');

            //var checkedRows = $('#productDg').iDatagrid('getChecked');
            //console.log(checkedRows);

            //var options = $('#myDg').iDatagrid('options');
            //var selectedRow = $('#myDg').iDatagrid('getSelected');
            //console.log(selectedRow);

            // 提交参数查询表格数据
            //$('#productDg').iDatagrid('reload', {
            //    name: $('#name').iTextbox('getValue'),
            //    code: $('#code').iTextbox('getValue')
            //});
        }
    }
</script>

<!--全局变量和公共方法-->
<script>

// 处理添加和编辑的时候load之后调用的方法
function addOrEditLoad(type,uuid) {
    if (type == "add") {
    }

    if (type == "edit") {
    }
}

// 保存时用户的自定义验证
function userValidate() {
    //var myFormProductGUIDValue = $("#myFormProductGUID").iCombobox("getValue");
    //if (myFormProductGUIDValue.length != 36) {
    //    layer.msg("@L("请选择对应产品")");
    //    return false;
    //}

    return true;
}
</script>

<!--表格操作-->
<script>
    $(function () {
        $("#mySearchForm").show();
     })

    // 定义初始化过滤条件
    var myDgInitFilter = null;
    var functionGUID = $.dm.getQueryString("functionGUID")
    if (functionGUID) {
        myDgInitFilter = {
            Type: "and",
            conditions: [{
                Attribute: "functionGUID",
                Datatype: "uniqueidentifier",
                Operatoer: "eq",
                Value: functionGUID
            }]
        };
    }

    function operateFormatter(value, row, index) {
        var htmlStr = '<button class="layui-btn layui-btn-xs" onclick="openDetailDiag(\'' + row.id + '\')">查看</button>';
        @if (currentUserInfo.IsHaveRights("FW_Action", "02"))
        {
            @:htmlStr += '<button class="layui-btn layui-btn-xs" onclick="openEditDiag(\'' + row.id + '\')">编辑</button>';
        }

        @if (currentUserInfo.IsHaveRights("FW_Action", "03"))
        {
            @:htmlStr += '<button class="layui-btn layui-btn-xs layui-btn-danger" onclick="deleteRow(\'' + row.id + '\')">删除</button>';
        }

        return htmlStr;
    }


    var dg = $('#myDg').iDatagrid({
        url: '/api/services/app/FWAction/GetGridDto',
        fitColumns: true,
        queryParams: {
            order: 'actionCode  asc',
            viewName: 'view_FW_Action',
            filter: JSON.stringify(myDgInitFilter)
        },
        columns: [
            [
                {
                    field: 'id',
                    title: 'id',
                    checkbox: true,
                },

                {
                    field: 'functionGUID',
                    title: '@L("对应的功能GUID")',
                    hidden: true,
                },

                {
                    field: 'actionCode',
                    title: '@L("动作代码")',
                    width: 120,
                    sortable: true,
                },

                {
                    field: 'actionName',
                    title: '@L("动作名称")',
                    width: 120,
                    sortable: true,
                },

                {
                    field: 'actionRemark',
                    title: '@L("动作说明")',
                    width: 120,
                    sortable: true,
                },

                //{
                //    formatter: operateFormatter,
                //    field: 'createTime',
                //    title: '@L("操作")',
                //    sortable: true,
                //    width: 100
                //}
            ]
        ],
        frozenColumns: [[

        ]],
        onDblClickRow: function (a, b) {
            @if (currentUserInfo.IsHaveRights("FW_Action", "02")){
            <text>
            openEditDiag(b.id);
            </text>}
        },
        filter: [
            {
                field: 'actionCode',
                type: 'textbox',
                op: ['contains', 'equal', 'notequal', 'endwith', 'beginwith']
            },

            {
                field: 'actionName',
                type: 'textbox',
                op: ['contains', 'equal', 'notequal', 'endwith', 'beginwith']
            },

            {
                field: 'actionRemark',
                type: 'textbox',
                op: ['contains', 'equal', 'notequal', 'endwith', 'beginwith']
            },

        ]
    });

    function searchQuery() {
        var param = $('#myDg').iDatagrid('options').queryParams;
        var filter = $.ei.getFilters();
        // 如果之前有filter的话那么把condtion加入到里面去
        if (myDgInitFilter&&myDgInitFilter.conditions) {
            if (filter) {
                filter.conditions = _.union(filter.conditions, myDgInitFilter.conditions);
            } else {
                filter = myDgInitFilter;
            }
        }
        if (filter) {
            param.filter = JSON.stringify(filter);
        }else {
            param.filter = null;
        }
        $('#myDg').iDatagrid('load', param);
    }
</script>


<!--按钮相关-->
<script>
     function oepnAddDiag() {
        var $editDialog = $('<form id="myFormDialog"></form>'); // 创建form表单元素
        $editDialog.iDialog({
            title: '@L("新增动作表")',
            width: 650,
            height: 400,
            closed: false,
            cache: false,
            href: '/FW/FWAction/FWActionEdit?mode=1',
                buttons: [{
                text: '@L("保存")',
                iconCls: 'fa fa-save',
                btnCls: 'topjui-btn-blue',
                handler: function () {
                    saveDate("saveAndClose");
                }
            },
            //{
            //    text: '@L("保存&新增")',
            //    iconCls: 'fa fa-save',
            //    btnCls: 'topjui-btn-blue',
            //    handler: function () {
            //        saveDate("saveAndNew");
            //    }
            //}, {
            //    text: '@L("保存&关闭")',
            //    iconCls: 'fa fa-save',
            //    btnCls: 'topjui-btn-blue',
            //    handler: function () {
            //        saveDate("saveAndClose");
            //    }
            //},
            {
                text: '@L("关闭")',
                iconCls: 'fa fa-close',
                btnCls: 'topjui-btn-red',
                handler: function () {
                    $('#myFormDialog').iDialog('close');// 销毁dialog面
                }
            }],
            onLoad: function () {
                addOrEditLoad("add");
                var functionGUID = $.dm.getQueryString("functionGUID");
                $editDialog.iForm('load', { functionGUID: functionGUID });
                //var options = {
                //    url: '/api/services/app/FWAction/GetViewDto',
                //    maskJqSelect: '#myFormDialog'
                //};

                //var success = function (result) {
                //    $editDialog.iForm('load', result);
                //}

                //abp.ajax(options).done(success);
            }
        });
    }

    function openEditDiag(uuid) {
        if (!uuid) {
            var selectedRow = $('#myDg').iDatagrid('getSelected');
            uuid = selectedRow.id;
        }
        var $editDialog = $('<form id="myFormDialog"></form>'); // 创建form表单元素
        $editDialog.iDialog({
            title: '@L("编辑动作表")',
            width: 650,
            height: 400,
            closed: false,
            cache: false,
            href: '/FW/FWAction/FWActionEdit?uuid=' + uuid+'&mode=2',
            // modal: true,
                buttons: [{
                text: '@L("保存")',
                iconCls: 'fa fa-save',
                btnCls: 'topjui-btn-blue',
                handler: function () {
                    saveDate("saveAndClose");
                }
            },
            //{
            //    text: '@L("保存&关闭")',
            //    iconCls: 'fa fa-save',
            //    btnCls: 'topjui-btn-blue',
            //    handler: function () {
            //        saveDate("saveAndClose");
            //    }
            //},
            {
                text: '@L("关闭")',
                iconCls: 'fa fa-close',
                btnCls: 'topjui-btn-red',
                handler: function () {
                    $('#myFormDialog').iDialog('close');// 销毁dialog面
                }
            }],
            onLoad: function () {
                addOrEditLoad("edit");
                var data = { 'id': uuid };
                var options = {
                    url: '/api/services/app/FWAction/GetViewDto',
                    maskJqSelect: '#myFormDialog',
                    data:data
                };

                var success = function (result) {
                    $editDialog.iForm('load', result);

                    // 绑定combox
                    $editDialog.find('[data-toggle="topjui-combobox"]').each(function (b) {
                        var c = $(this);
                        var opt = getOptionsJson(c);
                        if (opt.queryParams.viewName) {
                            c.iCombobox(opt);
                        }
                    });

                    // 在这里进行绑定UeEditor
                    $editDialog.find('[data-toggle="topjui-ueditor"]').each(function (b) {
                        var c = $(this);
                        var name = c.attr('name');
                        var opt = getOptionsJson(c);
                        var id = opt.id;
                        var ue = UE.getEditor(id);
                        ue.ready(function () {
                            ue.setContent(result[name]);
                        });
                    });
                }

                abp.ajax(options).done(success);
            }
        });
    }

    // operate表示操作类型有 save saveAndClose saveAndNew
    function saveDate(operate) {
        // 1. 显示进度条（提交时请使用进度条 或者 禁用'提交'按钮，防止因为网络延迟用户重复提交）
        $.iMessager.progress({ 'text': '@L("保存中...")' });
        var isValid = $('#myFormDialog').iForm('validate'); //  提交数据表单数据时进行表单验证
        var isUserValid = userValidate();
        if (!isValid || !isUserValid) {
            layer.msg('@L("格式不正确,请填写对应的必填项!")');
            $.iMessager.progress('close');  // 如果表单是无效的则隐藏进度条
            return;
        }

        var data = $.dm.parseQueryString($('#myFormDialog').serialize());
        var options = {
            url: '/api/services/app/FWAction/CreateByDto',
            data: data
        };

        // 有ID就是更新
        if (data.id) {
            options.url = '/api/services/app/FWAction/UpdateByDto'
        }
        var success = function (result) {
            $("#myDg").iDatagrid('reload'); // 刷新下表格数据
            $('#myFormDialog').iForm('load', { id: result.id });
            // 保存并关闭
            if (operate === 'saveAndClose') {
                $.iMessager.show({ title: '@L("我的消息")', msg: '@L("保存成功")', timeout: 2000, showType: 'slide' });// '消息窗口'组件
                $('#myFormDialog').iDialog('close');// 销毁dialog面板
            } else if (operate === 'saveAndNew') {
                // 保存新增重置form
                $('#myFormDialog').iForm('reset');
                layer.msg("@L("保存成功")");
            } else {
                layer.msg("@L("保存成功")");
            }
        }
        abp.ajax(options).done(success);
    }

    function openDetailDiag(uuid) {
        var $editDialog = $('<form id="myDetailDialog"></form>'); // 创建form表单元素
        $editDialog.iDialog({
            title: '@L("查看动作表")',
            width: 750,
            height: 550,
            closed: false,
            cache: false,
            href: '/FW/FWAction/FWActionDetail?uuid=' + uuid,
            buttons: [ {
                text: '@L("关闭")',
                iconCls: 'fa fa-close',
                btnCls: 'topjui-btn-red',
                handler: function () {
                    $('#myDetailDialog').dialog('close');// 销毁dialog面
                }
            }],
            onLoad: function () {
                var data = { 'id': uuid };
                var options = {
                    url: '/api/services/app/FWAction/GetViewDto',
                    maskJqSelect: '#myDetailDialog',
                    data : data
                };

                var success = function (result) {
                        $("#FWActionDetailTableTemplate").tmpl(result)
                        .appendTo("#FWActionDetailTable");
                }
                abp.ajax(options).done(success);
            }
        });
    }

    // 删除行数据
    function deleteRow(id) {
        $.messager.confirm("@L("提示")", "@L("确定要删除该条数据吗?")", function (result) {
            if (result) {
                $.iMessager.progress({ 'text': '@L("处理中...")' });
                var data={'id':id};
                var options = {
                    url: '/api/services/app/FWAction/DeleteByDto',
                    maskJqSelect: '#myDg',
                    data: data
                };

                var success = function (result) {
                    $("#myDg").iDatagrid('reload');// 刷新下表格数据
                    $.iMessager.show({ title: '@L("我的消息")', msg: '@L("删除成功")', timeout: 2000, showType: 'slide' });// '消息窗口'组件
                }

                abp.ajax(options).done(success);
            }
        })
    }

    function initActions() {
          $.messager.confirm("@L("提示")", "@L("确定要初始化权限点吗?")", function (result) {
              var options = {
                  url: '/api/services/app/FWAction/InitAction',
                  data: { functionGUID: $.dm.getQueryString("functionGUID") }
              };

              var success = function (result) {
                  $("#myDg").iDatagrid('reload');// 刷新下表格数据
                  $.iMessager.show({ title: '@L("我的消息")', msg: '@L("操作成功")', timeout: 2000, showType: 'slide' });// '消息窗口'组件
              }

              abp.ajax(options).done(success);
          });
    }

    // 自定义方法
    function myQuery() {
        alert("123");
        // 选择一行
        $('#myDg').datagrid('selectRow', 1);
        // 提示信息
        //$.iMessager.alert('自定义方法', '自定义方法被执行了！', 'messager-info');

        //var checkedRows = $('#productDg').iDatagrid('getChecked');
        //console.log(checkedRows);

        //var options = $('#myDg').iDatagrid('options');
        //var selectedRow = $('#myDg').iDatagrid('getSelected');
        //console.log(selectedRow);

        // 提交参数查询表格数据
        //$('#productDg').iDatagrid('reload', {
        //    name: $('#name').iTextbox('getValue'),
        //    code: $('#code').iTextbox('getValue')
        //});
    }
</script>
}
else
{
    <h1>@L("你没有该页面的权限")</h1>
}
